package com.shangcheng.dubbo.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shangcheng.dubbo.exception.BusinessException;
import com.shangcheng.dubbo.mappers.PmsProductCategoryAttributeRelationMapper;
import com.shangcheng.dubbo.mappers.PmsProductCategoryMapper;
import com.shangcheng.dubbo.mappers.PmsProductMapper;
import com.shangcheng.model.domain.PmsProduct;
import com.shangcheng.model.domain.PmsProductCategory;
import com.shangcheng.model.domain.PmsProductCategoryAttributeRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class PmsProductCategoryApiImpl implements PmsProductCategoryApi {
    @Autowired
    private PmsProductCategoryMapper productCategoryMapper;

    @Autowired
    private PmsProductMapper productMapper;

    @Autowired
    private PmsProductCategoryAttributeRelationMapper pmsProductCategoryAttributeRelationMapper;

    @Override
    public IPage<PmsProductCategory> pageList(Integer pageNum, Integer pageSize, Integer parentId) {
        IPage<PmsProductCategory> page = new Page<>(pageNum, pageSize);
        return productCategoryMapper.selectPage(page, new LambdaQueryWrapper<PmsProductCategory>().eq(PmsProductCategory::getParentId, parentId));
    }

    @Override
    public String create(PmsProductCategory productCategory, List<Long> productAttributeIdList) {
        PmsProductCategory one = productCategoryMapper.selectOne(new LambdaQueryWrapper<PmsProductCategory>().eq(PmsProductCategory::getName, productCategory.getName()));
        if (ObjectUtil.isNotEmpty(one)) {
            return "商品分类名称已存在";
        }
        productCategory.setProductCount(0);
        if (productCategory.getParentId() == 0) {
            productCategory.setLevel(0);
        } else {
            productCategory.setLevel(1);
        }
        productCategoryMapper.insert(productCategory);
        if (ObjectUtil.isNotEmpty(productAttributeIdList)) {
            for (Long productAttributeId : productAttributeIdList) {
                PmsProductCategoryAttributeRelation pmsProductCategoryAttributeRelation = new PmsProductCategoryAttributeRelation();
                pmsProductCategoryAttributeRelation.setProductCategoryId(productCategory.getId());
                pmsProductCategoryAttributeRelation.setProductAttributeId(productAttributeId);
                pmsProductCategoryAttributeRelationMapper.insert(pmsProductCategoryAttributeRelation);
            }
        }
        return "ok";
    }

    @Override
    public String update(PmsProductCategory productCategory, List<Long> productAttributeIdList) {
        PmsProductCategory one = productCategoryMapper.selectOne(new LambdaQueryWrapper<PmsProductCategory>().eq(PmsProductCategory::getName, productCategory.getName()));
        if (ObjectUtil.isNotEmpty(one)) {
            if (!one.getId().equals(productCategory.getId())) {
                return "商品分类名称已存在";
            }
        }
        productCategoryMapper.updateById(productCategory);
        if (ObjectUtil.isNotEmpty(productAttributeIdList)) {
            pmsProductCategoryAttributeRelationMapper.delete(new LambdaQueryWrapper<PmsProductCategoryAttributeRelation>()
                    .eq(PmsProductCategoryAttributeRelation::getProductCategoryId, productCategory.getId()));
            for (Long productAttributeId : productAttributeIdList) {
                PmsProductCategoryAttributeRelation pmsProductCategoryAttributeRelation = new PmsProductCategoryAttributeRelation();
                pmsProductCategoryAttributeRelation.setProductCategoryId(productCategory.getId());
                pmsProductCategoryAttributeRelation.setProductAttributeId(productAttributeId);
                pmsProductCategoryAttributeRelationMapper.insert(pmsProductCategoryAttributeRelation);
            }
        }
        return "ok";
    }

    @Override
    public void showStatus(Integer ids, Integer showStatus) {
        if (ObjectUtil.isNotEmpty(ids)) {
            PmsProductCategory productCategory = productCategoryMapper.selectById(ids);
            productCategory.setShowStatus(showStatus);
            productCategoryMapper.updateById(productCategory);
        }
    }

    @Override
    public void navStatus(Integer ids, Integer navStatus) {
        if (ObjectUtil.isNotEmpty(ids)) {
            PmsProductCategory productCategory = productCategoryMapper.selectById(ids);
            productCategory.setNavStatus(navStatus);
            productCategoryMapper.updateById(productCategory);
        }
    }

    @Override
    public PmsProductCategory findById(Integer id) {
        return productCategoryMapper.selectById(id);
    }

    @Override
    public String delete(Integer id) {
        List<PmsProductCategory> productCategoryList = productCategoryMapper.selectList(new LambdaQueryWrapper<PmsProductCategory>().eq(PmsProductCategory::getParentId, id));
        List<PmsProduct> pmsProductList = productMapper.selectList(new LambdaQueryWrapper<PmsProduct>().eq(PmsProduct::getProductCategoryId, id));
        if (ObjectUtil.isNotEmpty(productCategoryList)) {
            return "该分类还有子分类，不能直接删除";
        }
        if (ObjectUtil.isNotEmpty(pmsProductList)) {
            return "该分类与商品关联，不能直接删除";
        }
        productCategoryMapper.deleteById(id);
        return "ok";
    }

    @Override
    public List<PmsProductCategory> withChildren(Long parentId) {
        return productCategoryMapper.selectList(new LambdaQueryWrapper<PmsProductCategory>().eq(PmsProductCategory::getParentId, parentId));
    }
}
